import {
  addProfession,
  deleteProfession,
  updateProfession,
  queryProfession,
} from '../services/profession';

export default {
  namespace: 'profession',

  state: {
    ProfessionList: [],
  },

  effects: {
    *fetchProfessionList({ payload = {} }, { call, put }) {
      const response = yield call(queryProfession, payload);
      if (response.status) {
        yield put({
          type: 'saveProfessionList',
          payload: response.data,
        });
      }
    },

    *updateProfessionList({ payload = {} }, { call, put }) {
      const data = yield call(updateProfession, payload);
      if (data.status) {
        const dataLsit = yield call(queryProfession, {});
        yield put({
          type: 'saveProfessionList',
          payload: dataLsit.data,
        });
      }
    },

    *deleteProfessionList({ payload = {} }, { call, put }) {
      const data = yield call(deleteProfession, payload);
      if (data.status) {
        const dataLsit = yield call(queryProfession, {});
        yield put({
          type: 'saveProfessionList',
          payload: dataLsit.data,
        });
      }
    },

    *addProfessionList({ payload = {} }, { call, put }) {
      const data = yield call(addProfession, payload);
      if (data.status) {
        const dataLsit = yield call(queryProfession, {});
        yield put({
          type: 'saveProfessionList',
          payload: dataLsit.data,
        });
      }
    },
  },

  reducers: {
    saveProfessionList(state, { payload }) {
      return {
        ...state,
        ProfessionList: payload,
      };
    },
  },
};
